package com.opal.jx.service.impl;

import com.opal.consts.WSC;
import com.opal.domain.R;
import com.opal.jx.entity.JxAssessReception;
import com.opal.jx.entity.table.*;
import com.opal.jx.service.*;
import com.opal.system.service.OpalDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class JxIndexServiceImpl implements JxIndexService {

    @Autowired
    private OpalDeptService opalDeptService;
    @Autowired
    private JxEmpService jxEmpService;
    @Autowired
    private JxAssessContentService jxAssessContentService;
    @Autowired
    private JxPostService jxPostService;
    @Autowired
    private JxPZCJService jxAssessPzcjService;
    @Autowired
    private JxBZCJService jxBZCJService;

    @Override
    public Object getDeptByPkOrgService(String pkOrg) {
        return opalDeptService.getDeptEasyUiTreeData(pkOrg);
    }

    @Override
    public Object getPostByDeptService(String pkDept) {
        JxPost jxPost= new JxPost();
        jxPost.setPkDept(pkDept);
        jxPost.setPid(0l);  //0为最顶层的岗位
        List<JxPost> jxPostBycondition = jxPostService.getJxPostBycondition(jxPost);
        return jxPostBycondition;
    }

    @Override
    public Object selectSonPostService(String pkDept, String postName) {
        //根据岗位名称和部门找到此岗位的pid
        List<Long> ids= new ArrayList<>();

        JxPost jxPost = new JxPost();
        jxPost.setPkDept(pkDept);
        jxPost.setName(postName);
        List<JxPost> resultPost = jxPostService.getJxPostBycondition(jxPost);
        //根据查询到的id赋值到pid中来查询是否有值，若没有证明是父亲
        jxPost.setPid(resultPost.get(0).getId());
        jxPost.setPkDept(null);
        jxPost.setName(null);
        List<JxPost> resultSonPost = jxPostService.getJxPostBycondition(jxPost);

        if (resultSonPost.size()>0){  //等于0证明本身就是根的
            resultSonPost.stream().forEach(b->ids.add(b.getId()));
        }else {
            ids.add(resultPost.get(0).getId());
        }
        return new R(WSC.RESTPONSE_OK,"获取成功",ids);
    }

    @Override
    public Object getEmpByPkDeptAndPostId(String pkDept,Long postId) {
        JxEmp jxEmp =new JxEmp();
        jxEmp.setPkDept(pkDept);
        jxEmp.setId(postId);
        return jxEmpService.selectJxEmpByCondition(jxEmp);
    }

    @Override
    public Object getEmpByPkDeptAndPostIds(JxAssessReception jxAssessPzcjReception) { //就是辅助人员的子类id或它本生
        List<Long> postIds=new ArrayList<>();
        String postId= jxAssessPzcjReception.getPostId();
        if (postId.indexOf(",") >0){
            String[] split = postId.split(",");
            postIds = Arrays.stream(split).map(Long::parseLong).collect(Collectors.toList());
            //转换成list
        }else {
            postIds.add(Long.parseLong(postId));
        }

        //先在绩效表中查询是否有当月的数据
        Integer month = jxAssessPzcjReception.getMonth();
        Integer year = jxAssessPzcjReception.getYear();
        String pkDept = jxAssessPzcjReception.getPkDept();
        //因有父岗位，根据部门id和岗位名称找所有下一层的子岗位名称
        List<JxPost> jxPosts = jxPostService.getJxPostByIds(postIds);
        String[] postNames= new String[jxPosts.size()];
        for (int i=0;i<jxPosts.size();i++){
            postNames[i]=jxPosts.get(i).getName();
        }
        //根据年份、月份；岗位名称数组去x_assess_pzcj找
        List<JxAssessPzcj> jxAssessPzcjs = jxAssessPzcjService.selectJxAssessPzcjByCondition(year, month,postNames);

        if (jxAssessPzcjs.size()>0){
            return jxAssessPzcjs;
        }else {
            return jxEmpService.selectJxEmpByCondition(pkDept, postIds);
        }

    }

    @Override
    public Object getAssessContentDataService(Long postId) {
        List<JxAssessContent> jac = jxAssessContentService.getJxAssessContentByPostCode(postId);

        return jac;
    }

    @Override
    public Object getEmpInfoService(JxAssessReception jxAssessPzcjReception) {
        //首先查看JxAssessBzcj中有无数据
        JxAssessBzcj jb= new JxAssessBzcj();
        jb.setAssessMonth(jxAssessPzcjReception.getMonth());
        jb.setAssessYear(jxAssessPzcjReception.getYear());
        jb.setPostName(jxAssessPzcjReception.getPostName());
        List<JxAssessBzcj> jxAssessBzcjs = jxBZCJService.selectJxAssessBzcjByCondition(jb);

        return null;
    }

}
